﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Script.Services;
using System.Web.Services;
using System.ServiceModel.Description;
using System.ServiceModel;
using ZX.BLL.Extend;
using ZX.WebApp.CrmWorkFlowService;
namespace ZX.WebApp.Mobi
{
    public partial class WorkflowDataManager : ZX.Web.UI.Controls.BasePage    {
        protected void Page_Load(object sender, EventArgs e)
        {
          // SaveApproval(string UserName, string TransmitString,string UserNames,string AuditId, 
          // string WorkFlowId, string WorkTaskId, string WorkFlowInsId, string WorktaskInsId,
          //  string OperatorInsId, string ApprovalMessage, string ApprovalStatus)
          Response.Write(  SaveApproval(
                this.UserName,
                this.GetStringFromQueryString("TransmitString", ""),
                this.GetStringFromQueryString("UserNames", ""),
                this.GetStringFromQueryString("AuditId", ""),
                this.GetStringFromQueryString("WorkFlowId", ""),
                this.GetStringFromQueryString("WorkTaskId", ""),
                this.GetStringFromQueryString("WorkFlowInsId", ""),
                this.GetStringFromQueryString("WorktaskInsId", ""),
                this.GetStringFromQueryString("OperatorInsId", ""),
                this.GetStringFromQueryString("ApprovalMessage", ""),
                this.GetStringFromQueryString("ApprovalStatus", "")));
        }

        /// <summary>
        /// 签核实体
        /// </summary>
        [Serializable]
        public class ApprovalEntity
        {
            public string AuditId
            {
                get;
                set;
            }
            public string UserName
            {
                get;
                set;
            }
            public string Arch
            {
                get;
                set;
            }
            public string AuditTime
            {
                get;
                set;
            }
            
            public string Duty
            { get; set; }

            public string Status
            {
                get;
                set;
            } 
        }
        #region 附件
        #endregion 附件

        #region 审批意见
        /// <summary>
        /// 审批意见初始化 
        /// </summary>
        /// <param name="WorktaskId"></param>
        public  string ApprovalInit(string UserName,string OperatorInsId)
        {
          // username,workFlowId, workTaskId, workFlowInsId, worktaskInsId, operatorInsId
            ApprovalEntity app = new ApprovalEntity();

                if (!string.IsNullOrEmpty(OperatorInsId))
                {
                    ZX.WebApp.CrmWorkFlowService.WorkFlowServiceClient client = new CrmWorkFlowService.WorkFlowServiceClient();
                    BaseUserControl baseuser = new BaseUserControl();
                    DataTable dt = client.GetAuditMsgTableByOper(OperatorInsId);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        app.AuditId = dt.Rows[0]["AuditId"].ToString();
                        app.UserName = dt.Rows[0]["AuditUserName"].ToString();
                        app.Arch = dt.Rows[0]["AuditArch"].ToString();
                        app.Duty = dt.Rows[0]["AuditDuty"].ToString();
                        app.AuditTime=dt.Rows[0]["AuditTime"].ToString();
                        app.Status= WorkFlowConst.STATE_MOD;
                    }
                    else
                    {
                        app.AuditId = Guid.NewGuid().ToString();
                        app.UserName = UserName;
                        app.Arch = baseuser.ArchCaption;
                        app.Duty = baseuser.DutyCaption;
                        app.AuditTime = DateTime.Now.ToShortDateString();
                        app.Status = WorkFlowConst.STATE_ADD;
                    }
                }
            return ZX.Common.Globals.ObjectToJSON(app);
        }
        /// <summary>
        /// 保存审批意见
        /// </summary>
        /// <param name="UserName">审批人</param>
        /// <param name="TransmitString">转发字串</param>
        /// <param name="UserNames">转发人姓名字符串</param>
        /// <param name="AuditId">审批记录编号</param>
        /// <param name="WorkFlowId">流程编号</param>
        /// <param name="WorkTaskId">任务编号</param>
        /// <param name="WorkFlowInsId">流程实例编号</param>
        /// <param name="WorktaskInsId">流程任务编号</param>
        /// <param name="OperatorInsId">处理者编号</param>
        /// <param name="ApprovalMessage">审批意见</param>
        /// <param name="ApprovalStatus">审批记录状态</param>
        /// <returns></returns>
        public  string SaveApproval(string UserName, string TransmitString,string UserNames,string AuditId, 
            string WorkFlowId, string WorkTaskId, string WorkFlowInsId, string WorktaskInsId,
            string OperatorInsId, string ApprovalMessage, string ApprovalStatus)
        {
            // username,workFlowId, workTaskId, workFlowInsId, worktaskInsId, operatorInsId
            string strWorkMsg = "";
            try
            {
                ZX.EHR.Library.DatUser U = HttpContext.Current.Session["User"] as ZX.EHR.Library.DatUser;
                ZX.WebApp.CrmWorkFlowService.WorkFlowServiceClient client = new CrmWorkFlowService.WorkFlowServiceClient();
                //调用转发
                if (!string.IsNullOrEmpty(TransmitString))
                {
                    string[] varuser = TransmitString.Split(',');
                    
                    for (int i = 0; i < varuser.Length; i++)
                    {
                        string loginName=varuser[i].ToString();
                        HttpContext.Current.Session["transmitId"] = UserNames;
                        if (!string.IsNullOrEmpty(loginName))
                        {
                            if (loginName != U.LoginName)//不能转发给自己。
                            {
                                ZX.BLL.BUser buser = new ZX.BLL.BUser();
                                ZX.WebApp.CrmWorkFlowService.WorkTaskInstanceNextOperEntity entity = new ZX.WebApp.CrmWorkFlowService.WorkTaskInstanceNextOperEntity();
                                entity.NextOperId = Guid.NewGuid().ToString();
                                entity.UserId = varuser[i].ToString();
                                entity.UserName = "";
                                entity.WorkFlowId = WorkFlowId;
                                entity.WorkTaskId = WorkTaskId;
                                entity.WorkFlowInstanceId = WorkFlowInsId;
                                entity.WorkTaskInstanceId = WorktaskInsId;
                                entity.Status = "0";
                                client.NextOperInsert(entity);
                            }
                        }
                    }
                }

                //保存签核意见
                
                CrmWorkFlowService.AuditMessage am = new CrmWorkFlowService.AuditMessage();
                am.AuditId = AuditId;
                am.WorkflowId = WorkFlowId;
                am.WorkflowInsId = WorkFlowInsId;
                am.WorktaskId = WorkTaskId;
                am.WorktaskInsId = WorktaskInsId;
                am.OperatorInsId = OperatorInsId;
                am.AuditUserName = UserName;
                BaseBussnies basebussnies = new BaseBussnies();
                DataTable dt = basebussnies.GetDeptandDuty(U.EmpID.ToString());
                if (dt.Rows.Count > 0)
                {
                    am.AuditArch = dt.Rows[0]["DeptName"].ToString();
                    am.AuditDuty = dt.Rows[0]["PostName"].ToString();
                }
                am.Message = ApprovalMessage;
                DataTable dtTaskInsId = client.GetWorktaskInsStatus(WorktaskInsId);

                if (ApprovalStatus == WorkFlowConst.STATE_ADD)
                {
                    client.InsertAuditMessage(am);
                }
                else if (dtTaskInsId.Rows.Count > 0)
                {
                    client.InsertAuditMessage(am);
                }
                else
                {
                    client.UpdateAuditMessage(am);
                }
                strWorkMsg = "OK%@";
            }
            catch (Exception ex)
            {
                strWorkMsg = "ERR%@" + ex.Message;
            }
            return strWorkMsg;
        }
        #endregion 审批意见
    }
}